/*
 * 
 */
package darksideoftheloom;

import java.util.List;
import java.util.LinkedList;

/**
 * 
 * @author Roger Trottier
 * @version 2/20/2013
 * 
 * Event class stores information on all user's for an event.
 *
 */
public class Event {

	/**
	 * my_s is a list of s including contestant and judge.
	 */
	private List<Attendee> my_attendees;
	
	/**
	 * the organizer of the event.
	 */
	private Organizer my_organizer;
	
	/**
	 * the validity of all registered fields
	 */
	private boolean my_validity = false;
	
	/**
	 * the constructor that initializes my_s and my_submissions.
	 */
	public Event() {
		my_attendees = new LinkedList<Attendee>();
		my_organizer = new Organizer("all mighty bob", "password");
	}
	
	/**
	 * the constructor that initializes my_s and my_submissions.
	 * 
	 * @param the_s a list of s including contestant and judge.
	 * @param the_submissions a list of submissions that my_s (actors) submit.
	 */
	public Event(List the_attendee) {
		my_attendees = the_attendee;
	}
	
	/**
	 * Adds an attendee to the list of attendees.
	 * 
	 * @param the_name the name of the attendee.
	 * @param the_phone_number the phone number of the attendee.
	 * @param the_address the mailing address of the attendee.
	 * @param the_email the e-mail address of the attendee.
	 * @param the_username the user name of the attendee's account.
	 * @param the_password the password of the attendee's account.
	 */
	public void addAttendee(String the_name, String the_phone_number, String the_address, 
			String the_email, String the_username, String the_password) {
		Attendee a_contestant = new Attendee(the_name, the_phone_number, the_address, the_email, 
				the_username, the_password);
		int phone_number = 0;
		boolean email_validity = false;
		
		for (int i = 0; i < the_phone_number.length(); i++) {
			int ascii = the_phone_number.charAt(i) - 47;
			if (ascii > 0 && ascii < 11)
				phone_number++;
		}
		for (int i = 0; i < the_email.length(); i++) {
			int ascii = the_email.charAt(i);
			if (ascii == 64) {
				email_validity = true;
				break;
			}
		}
		
		if (the_name != null && (phone_number == 7 || phone_number == 10 || 
				phone_number == 11) && the_address != null && email_validity && 
				the_username != null && the_password != null) {
				my_attendees.add(a_contestant);
				my_validity = true;
		}
	}
	
	/**
	 * This method converts a contestant into a judge or changes a current judges category.
	 * 
	 * @param the_username the name of the user that is becoming a judge.
	 * @param the_catagory the category the user will judge.
	 */
	//TODO option for judge to judge multiple catagories in the future.
	public void addJudge(String the_username, String the_catagory) {
		int count = 0;
		while (my_attendees.get(count).getUsername() != the_username || 
				0  < my_attendees.size()) {
			count++;
		}
		if (my_attendees.get(count).getUsername() == the_username) {
			 Attendee temp_attendee = my_attendees.get(count);
			 Attendee a_judge = new Judge(temp_attendee.getName(), temp_attendee.getPhoneNumber(), 
					temp_attendee.getAddress(), temp_attendee.getEmail(), temp_attendee.getName(),
					temp_attendee.getPassword(), the_catagory);
			 my_attendees.remove(count);
			 my_attendees.add(a_judge);
		}
	}
	
	
	/**
	 * removes an attendee, this should only be accessed by the organizer!
	 * 
	 * @param the_attendee the name of the attendee being removed.
	 */
	public void removeAttendee(String the_attendee) {
		int count = 0;
		while (my_attendees.get(count).getUsername() != the_attendee || 
				0  < my_attendees.size()) {
			count++;
		}
		if (my_attendees.get(count).getName() == the_attendee) {
			my_attendees.remove(count);	
		}
	}
	
	/**
	 * addSubmission adds a submission to the attendee object.
	 * 
	 * @param the_attendee the current attendee object a submission is being added to.
	 * @param the_design_name the name of the design added to the attendee.
	 * @param the_category the name of the category the submission belongs to
	 * @param the_draft the weaving draft object being stored in the submission.
	 */
	public void addSubmission(Attendee the_attendee, String the_design_name, 
			String the_category, WeavingDraft the_draft) {
		Submission a_submission = new Submission(the_attendee.getUsername(), the_design_name,
				the_category, the_draft);
		the_attendee.submit(a_submission);
	}
	
	
	/*
	//WAIT!!!
	
	public void removeSubmission(String the_submission_name) {
		int count = 0;
		while (my_attendee.get(count).getUsername() != the_username || 
				0  < my_attendee.size()) {
			count++;
		}
		
	}
	
	public Attendee getAttendee(String the_attendee_name) {
		
	}
	*/
	
	public Organizer getOrganizer() {
		return my_organizer;
	}
	
	public List<Attendee> getAttendees() {
		return my_attendees;
	}
	
	public boolean getValidity() {
		boolean result = my_validity;
		my_validity = false;
		return result;
	}
	
	public void save() {
		DataStorage a_storage = new DataStorage();
		a_storage.save(my_attendees, my_organizer);
	}
	
	public void load() {
		DataStorage a_storage = new DataStorage();
		my_attendees = a_storage.getAttendees();
		my_organizer = a_storage.getOrganizer();
	}
}
